/**
 * 菜单路由模块
 */

const express = require('express');
const menuController = require('../controllers/menuController');
const { authenticateToken } = require('../middlewares/authMiddleware');
const { checkPermission, checkSelfOrPermission ,checkPermissionLevel} = require('../middlewares/permissionMiddleware');

const router = express.Router();

// 所有用户路由都需要认证Token
router.use(authenticateToken);

// 数据域类接口（通用接口，需要控制范围）

// 根据用户ID获取菜单
router.get('/user/:id', checkSelfOrPermission('menu:view', { paramKey: 'id', minLevel: 40 }), menuController.getMenusByUserId);


// 管理类接口（需要管理员权限）

// 获取菜单列表
router.get('/', checkPermission('menu:list'),checkPermissionLevel(40), menuController.getAllMenus);

// 获取菜单树结构
router.get('/tree', checkPermission('menu:list'),checkPermissionLevel(40), menuController.getMenuTree);

// 创建菜单
router.post('/', checkPermission('menu:create'),checkPermissionLevel(40), menuController.createMenu);

// 获取单个菜单详情
router.get('/:id', checkPermission('menu:view'), checkPermissionLevel(40), menuController.getMenu);

// 更新菜单
router.put('/:id', checkPermission('menu:update'), checkPermissionLevel(40), menuController.updateMenu);

// 删除菜单
router.delete('/:id', checkPermission('menu:delete'), checkPermissionLevel(40), menuController.removeMenu);

// 更新单个菜单排序
router.put('/:id/sort', checkPermission('menu:update'), checkPermissionLevel(40), menuController.updateMenuSort);

// 批量更新菜单排序
router.put('/batch-sort', checkPermission('menu:update'), checkPermissionLevel(40), menuController.batchUpdateMenuSort);

module.exports = router;